<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="!!model.id" label="ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['id']" ></a-input>
        </a-form-item>
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item label="频道" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['channelId']" :trigger-change="true" dictCode="ads_channel,name,id" placeholder="请选择频道"/>
        </a-form-item>
        <a-form-item label="终端类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['mediaType', validatorRules.mediaType]" :trigger-change="true" dictCode="ads_media_type" placeholder="请选择终端类型"/>
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag
            type="list" v-decorator="['type', validatorRules.type]"
            @change="val => model.type=Number(val)"
            :trigger-change="true" dictCode="ads_slot_type" placeholder="请选择类型"/>
        </a-form-item>
        <a-form-item label="尺寸类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['sizeTypeName']"
            :trigger-change="true"
            org-fields="name,width,height,position"
            dest-fields="sizeTypeName,width,height,position"
            code="ads-size-type"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="宽 px" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="!!model.sizeTypeName" v-decorator="['width', validatorRules.width]" placeholder="请输入宽 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="高 px" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="!!model.sizeTypeName" v-decorator="['height', validatorRules.height]" placeholder="请输入高 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="位置说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['position']" placeholder="请输入位置说明"></a-input>
        </a-form-item>
        <a-form-item label="水平对齐" v-show="[2,3].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['alignH']" :trigger-change="true" dictCode="ads_align_h" placeholder="请选择水平对齐"/>
        </a-form-item>
        <a-form-item label="水平边距 px" v-show="[2,3].includes(model.type)"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['marginH', validatorRules.marginH]" placeholder="请输入水平边距 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="垂直对齐" v-show="[2,3].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['alignV']" :trigger-change="true" dictCode="ads_align_v" placeholder="请选择垂直对齐"/>
        </a-form-item>
        <a-form-item label="垂直边距 px" v-show="['2','3'].includes(model.type)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['marginV', validatorRules.marginV]" placeholder="请输入垂直边距 px" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="停留时间（秒）" v-show="model.type!==1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['stayTime', validatorRules.stayTime]" placeholder="请输入停留时间（秒）" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="跟随滚动条" v-show="model.type===2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['scrolled', validatorRules.scrolled]" :trigger-change="true" dictCode="yn" placeholder="请选择是否跟随滚动条"/>
        </a-form-item>
        <a-form-item label="有关闭按钮" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['closable', validatorRules.closable]" :trigger-change="true" dictCode="yn" placeholder="请选择是否有关闭按钮"/>
        </a-form-item>
        <a-form-item label="调度策略" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['scheduleMode', validatorRules.scheduleMode]" :trigger-change="true" dictCode="ads_schedule_mode" placeholder="请选择调度策略"/>
        </a-form-item>
        <a-form-item label="轮播广告数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number
            v-decorator="['adCount', validatorRules.adCount]"
            @change="val => model.adCount=Number(val)"
            placeholder="请输入一次展示广告数" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="轮换间隔（秒）" v-show="model.adCount>1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['rotateInterval', validatorRules.rotateInterval]" placeholder="请输入轮换间隔（秒）" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="每人每天显示次数" v-show="model.type!==1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['dayTimes', validatorRules.dayTimes]" placeholder="请输入每天显示次数" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['enabled', validatorRules.enabled]" :trigger-change="true" dictCode="yn" placeholder="请选择启用"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
        </a-form-item>

        <a-form-item label="创建者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createBy']" ></a-input>
        </a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="修改者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateBy']"></a-input>
        </a-form-item>
        <a-form-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateTime']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "SlotModal",
    components: {
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {
            rules: [
              { required: true, message: '请输入名称!'},
            ]
          },
          mediaType: {
            rules: [
              { required: true, message: '请输入终端类型!'},
            ],
            initialValue: 'all'
          },
          type: {
            rules: [
              { required: true, message: '请输入类型!'},
            ]
          },
          width: {
            rules: [
              { required: true, message: '请输入宽 px!'},
            ]
          },
          height: {
            rules: [
              { required: true, message: '请输入高 px!'},
            ]
          },
          marginH: {
            rules: [
              { required: true, message: '请输入水平边距 px!'},
            ],
            initialValue: 0
          },
          marginV: {
            rules: [
              { required: true, message: '请输入垂直边距 px!'},
            ],
            initialValue: 0
          },
          stayTime: {
            rules: [
              { required: true, message: '请输入停留时间（秒）!'},
            ],
            initialValue: 0
          },
          scrolled: {
            rules: [
              { required: true, message: '请输入是否跟随滚动条!'},
            ],
            initialValue: 0
          },
          closable: {
            rules: [
              { required: true, message: '请输入是否有关闭按钮!'},
            ],
            initialValue: 0
          },
          scheduleMode: {
            rules: [
              { required: true, message: '请输入调度策略!'},
            ]
          },
          adCount: {
            rules: [
              { required: true, message: '请输入一次展示广告数!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ],
            initialValue: 1
          },
          rotateInterval: {
            rules: [
              { required: true, message: '请输入轮换间隔（秒）!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ],
            initialValue: 5
          },
          dayTimes: {
            rules: [
              { required: true, message: '请输入每个访客每天显示次数!'},
            ],
            initialValue: 0
          },
          enabled: {
            rules: [
              { required: true, message: '请输入启用!'},
            ],
            initialValue: 1
          },
        },
        url: {
          add: "/slot/add",
          edit: "/slot/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        //模板中v-if、v-show等中用到的数据的初始值，需在本处设置（v-decorator中的initialValue一开始未同步到model）
        this.edit({mediaType:'all', marginH:0, marginV:0, stayTime:0, scrolled:0, closable:0,
          adCount:0, rotateInterval:5, dayTimes:0, enabled:1});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'id','name','channelId','mediaType','type','sizeTypeName','width','height','position','alignH','marginH','alignV','marginV','stayTime','scrolled','closable','scheduleMode','adCount','rotateInterval','dayTimes','enabled','remark','createBy','createTime','updateBy','updateTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'id','name','channelId','mediaType','type','sizeTypeName','width','height','position','alignH','marginH','alignV','marginV','stayTime','scrolled','closable','scheduleMode','adCount','rotateInterval','dayTimes','enabled','remark','createBy','createTime','updateBy','updateTime'))
        this.model.sizeTypeName = row.sizeTypeName;
      },


    }
  }
</script>

<style lang="less" scoped>
  .ant-row.ant-form-item {
    margin-bottom: 12px;
  }
</style>